package com.mao.server.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mao.server.annotation.HoneyLogs;
import com.mao.server.constant.Constants;
import com.mao.server.domain.enumuric.LogType;
import com.mao.server.domain.po.News;
import com.mao.server.domain.po.User;
import com.mao.server.exception.ServiceException;
import com.mao.server.service.NewsService;
import com.mao.server.service.UserService;
import com.mao.server.utils.BaseContext;
import com.mao.server.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/news")
public class NewsController {

    @Autowired
    NewsService newsService;

    @Autowired
    UserService userService;

    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String title) {
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(title), Constants.TITLE, title);

        Page<News> page = newsService.page(new Page<>(pageNum, pageSize), queryWrapper);

        List<News> records = page.getRecords();
        for (News record : records) {
            Integer authorid = record.getAuthorid();
            User user = userService.getById(authorid);
            if (user != null) {
                record.setAuthor(user.getName());
            }
        }
        return Result.success(page);
    }

    @HoneyLogs(operation = "新闻", type = LogType.ADD)
    @PostMapping("/add")
    public Result add(@RequestBody News news) {
        Long currentId = BaseContext.getCurrentId();
        news.setAuthorid(Math.toIntExact(Long.valueOf(currentId)));
        news.setTime(DateUtil.now());
        newsService.save(news);
        return Result.success();
    }

    @HoneyLogs(operation = "新闻", type = LogType.DELETE)
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        newsService.removeById(id);
        return Result.success();
    }

    @HoneyLogs(operation = "新闻", type = LogType.UPDATE)
    @PutMapping("/update")
    public Result update(@RequestBody News news) {
        newsService.updateById(news);
        return Result.success();
    }

    @HoneyLogs(operation = "新闻", type = LogType.BATCH_DELETE)
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        newsService.removeBatchByIds(ids);
        return Result.success();
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        News news = newsService.getById(id);
        User user = userService.getById(news.getAuthorid());
        if (user != null) news.setAuthor(user.getName());
        return Result.success(news);
    }

    @GetMapping("/selectAll")
    public Result selectAll() {
        return Result.success(newsService.list());
    }
}
